package com.gupao.vip.nio.buffer;

import java.nio.ByteBuffer;

/**
 * 缓冲区分片
 * @创建人 majingang
 * @联系方式 aoyouxingkong08@163.com
 * @创建日期 2020-04-15
 * @描述
 */
public class BufferSlice {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);

        //缓冲区中的数据0-9
        for(int i=0; i<byteBuffer.capacity(); ++i){
            byteBuffer.put((byte)i);
        }

        //创建子缓冲区
        byteBuffer.position(3);
        byteBuffer.limit(7);
        ByteBuffer slice = byteBuffer.slice();
        //改变子缓冲区的内容
        for(int i=0; i<slice.capacity(); ++i){
            byte b = slice.get(i);
            b *= 10;
            slice.put(b);
        }
        byteBuffer.position(0);
        byteBuffer.limit(byteBuffer.capacity());
        while(byteBuffer.hasRemaining()){
            System.out.println(byteBuffer.get());
        }

    }
}
